<?php
/**
 *
 *
 *
 */
class ImageUtils 
{
	/**
	 *
	 *
	 * @param $filename
	 * @param $w
	 * @param $h
	 * @param $override
	 * @param $background
	 * @param $color
	 */
	public function RatioAdjuct($filename = '', $w = 440, $h = 300, $override = null, $background = null, $color = '0xFFFFFF') {
		list ( $imgWidth, $imgHeight ) = getImageSize ( $filename );
		
		$ratioX = $imgWidth / $w;
		$ratioY = $imgHeight / $h;
		
		if ($ratioX > $ratioY || $ratioX == $ratioY) {
			$dst_w = $w;
			$dst_h = ceil ( $imgHeight / $ratioX );
		} else if ($ratioY > $ratioX) {
			$dst_h = $h;
			$dst_w = ceil ( $imgWidth / $ratioY );
		}
		
		//
		switch (strtolower ( strrchr ( $filename, '.' ) )) {
			case '.jpg' :
			case '.jpeg' :
				$im = imageCreateFromJpeg ( $filename );
				break;
			case '.gif' :
				$im = imageCreateFromGif ( $filename );
				break;
			
			case '.png' :
				$im = imageCreateFromPng ( $filename );
		}
		
		//
		if (null !== $background) {
			//
			$dec = hexdec ( $color );
			$red = 0xFF & ($dec >> 0x10);
			$green = 0xFF & ($dec >> 0x8);
			$blue = 0xFF & $dec;
			
			//
			$dst_pos = array ('d_x' => 0, 'd_y' => 0 );
			($dst_w == $w) ? ($dst_pos ['d_y'] = (($h - $dst_h) / 2)) : ($dst_pos ['d_x'] = (($w - $dst_w) / 2));
			
			$imBox = imageCreateTrueColor ( $w, $h );
			$color_bg = imageColorAllocate ( $imBox, $red, $green, $blue );
			imageFill ( $imBox, 0, 0, $color_bg );
			imageCopyResized ( $imBox, $im, $dst_pos ['d_x'], $dst_pos ['d_y'], 0, 0, $dst_w, $dst_h, $imgWidth, $imgHeight );
		} else {
			$imBox = imageCreateTrueColor ( $dst_w, $dst_h );
			imageCopyResized ( $imBox, $im, 0, 0, 0, 0, $dst_w, $dst_h, $imgWidth, $imgHeight );
		}
		
		//
		if (null === $override)
			$filename = str_replace ( strrchr ( $filename, '.' ), '', $filename ) . '_thumb.png';
		
		return imagejpeg ( $imBox, $filename ) ? $filename : false;
	}
}

?>